﻿function sendMail(flag) {
    if (flag == "en") {
        $.colorbox({
            href: "en_pop_up.html",
            width: 640,
            height: 480
        });
    }
    else {
        $.colorbox({
            href: "cn_pop_up.html",
            width: 640,
            height: 480
        });
    } 
}

//var isWhat="cn";
function Check(isWhat) {
    var error = "";
    var surName = $("#surName").val();
    var personalName = $("#personalName").val();
    var userEmail = $("#userEmail").val();
    var moblie = $("#moblie").val();
    var friendName1 = $("#friendName1").val();
    var friendEmail1 = $("#friendEmail1").val();
    var friendName2 = $("#friendName2").val();
    var friendEmail2 = $("#friendEmail2").val();
    var friendName3 = $("#friendName3").val();
    var friendEmail3 = $("#friendEmail3").val();
    //验证用户名
    if (surName == "请输入姓氏" || surName == "firs name" || personalName == "请输入名称" || personalName == "last name")
        error += (isWhat == "cn") ? "请输入姓名!\n" : "Please enter fullname!\n";
    //验证用户邮箱
    if (userEmail == "请输入邮箱" || userEmail == "email address") {
        error += (isWhat == "cn") ? "请输入邮箱!\n" : "Please enter email!\n";
    }
    else { //验证邮箱格式
        if (!CheckEmail(userEmail)) {
            error += (isWhat == "cn") ? "邮箱格式不正确!\n" : "Mailbox format is incorrect!\n";
        }
        else { //验证邮箱唯一性
            var flag = IsExist(userEmail);
            //alert("flag"+flag);
        if (flag > 0)
                error += (isWhat == "cn") ? "邮箱已存在!\n" : "Mailbox already exists!\n";
        }
    }
    //验证手机
    if (moblie == "请输入手机号" || moblie == "mobile number") {
        error += (isWhat == "cn") ? "请输入手机号!\n" : "Please enter mobile number!\n";
    }
    else {
        //if (!/\d{11}/.test(moblie)) {
        if(!/^0?(13|15|18)[0-9]{9}$/.test(moblie)){
            error += (isWhat == "cn") ? "手机应为11位数字\n" : "Mobile phones should be 11 digits!\n";
        }
    }
    //朋友邮箱验证
    if ((friendName1 == "请输入姓名") || (friendName1 == "name of friend") || (friendEmail1 == "请输入邮箱") || (friendEmail1 == "email address")) {
        error += (isWhat == "cn") ? "请输入朋友姓名和邮箱!\n" : "Please enter your friend's name and mailbox!\n";
    }
    else {
        if (friendEmail1 != "请输入邮箱" && friendEmail1 != "email address") {
            if (!CheckEmail(friendEmail1))
                error += (isWhat == "cn") ? "朋友的邮箱格式不正确!\n" : "Friends of mailbox format is incorrect!\n";
        }
        if (friendEmail2 != "请输入邮箱" && friendEmail2 != "email address") {
            if (!CheckEmail(friendEmail2))
                error += (isWhat == "cn") ? "朋友的邮箱格式不正确!\n" : "Friends of mailbox format is incorrect!\n";
        }
        if (friendEmail3 != "请输入邮箱" && friendEmail3 != "email address") {
            if (!CheckEmail(friendEmail3))
                error += (isWhat == "cn") ? "朋友的邮箱格式不正确!\n" : "Friends of mailbox format is incorrect!\n";
        }
    }
    if (error != "") {
        alert(error);
        return false;
    }
    else {
        return true;
    }
}

var CheckEmail = function (email) {
    if (!/^\s*([A-Za-z0-9_-]+(\.\w+)*@([\w-]+\.)+\w{2,3})\s*$/.test(email))
        return false;
    else
        return true;
}
function IsExist(email) {
    try {
        var flag;
        $.ajax({
            type: "GET",
            url: "/sendMail.aspx",
            dataType: "text",
            async: false,
            data: "op=op&userEmail=" + email + "&random=" + new Date().toLocaleString(),
            beforeSend: function (xmlHttpRequest) {
            },
            success: function (data, textStatus) {
                //alert("isemail:" + data);
                flag = data
            },
            complete: function (xmlHttpRequest, textStatus) {

            },
            error: function () {
                flag = 0;
            }
        });
        return flag
    }
    catch (e) {
        alert("exception: " + e.Message);
    }
}

function submit(isWhat) {
    var surName = $("#surName").val();
    var personalName = $("#personalName").val();
    var moblie = $("#moblie").val();
    var userEmail = $("#userEmail").val();
    var friendName1 = $("#friendName1").val();
    var friendName2 = $("#friendName2").val();
    var friendName3 = $("#friendName3").val();
    
    var friendEmail1 = $("#friendEmail1").val();
    var friendEmail2 = $("#friendEmail2").val();
    var friendEmail3 = $("#friendEmail3").val();
    //alert('aa'+friendName1);
    if (friendName1 == "请输入姓名" || friendName1 == "name of friend")
        friendName1 = "";
    //alert(friendName1);
    
    if (friendName2 == "请输入姓名" || friendName2 == "name of friend")
        friendName2 = "";
    //alert(friendName2);
    
    if (friendName3 == "请输入姓名" || friendName3 == "name of friend")
        friendName3 = "";
    if (friendEmail1 == "请输入邮箱" || friendEmail1 == "email address")
        friendEmail1 = "";
   // alert(friendEmail1);
    if (friendEmail2 == "请输入邮箱" || friendEmail2 == "email address")
        friendEmail2 = "";
   // alert(friendEmail2);
    if (friendEmail3 == "请输入邮箱" || friendEmail3 == "email address")
        friendEmail3 = "";
    //alert(friendEmail3);
    
    if (Check(isWhat)) {
//        alert(isWhat);
//        alert(surName);
//        alert(personalName);
//        alert(userEmail);
//        alert(moblie);
        try {
        //alert("surName=" + encodeURIComponent(surName) + "&personalName=" + encodeURIComponent(personalName) + "&userEmail=" + userEmail + "&moblie=" + moblie + "&friendEmail1=" + friendEmail1 + "&friendEmail2=" + friendEmail2 + "&friendEmail3=" + friendEmail3 + "&lang=" + isWhat + "&friendName1=" + encodeURIComponent(friendName1) + "&friendName2=" + encodeURIComponent(friendName2) + "&friendName3=" + encodeURIComponent(friendName3));
            $.ajax({
                type: "GET",
                url: "/sendMail.aspx",
                dataType: "text",
                async: false,
                data: "surName=" + encodeURIComponent(surName) + "&personalName=" + encodeURIComponent(personalName) + "&userEmail=" + userEmail + "&moblie=" + moblie + "&friendEmail1=" + friendEmail1 + "&friendEmail2=" + friendEmail2 + "&friendEmail3=" + friendEmail3 + "&lang=" + isWhat + "&friendName1=" + encodeURIComponent(friendName1) + "&friendName2=" + encodeURIComponent(friendName2) + "&friendName3=" + encodeURIComponent(friendName3) + "&random=" + new Date().toLocaleString(),
                beforeSend: function (xmlHttpRequest) {
                },
                success: function (data, textStatus) {
                    //alert(data);
                    if (parseInt(data) > 0) {
                        $.fn.colorbox.close();
                        DownloadFile(isWhat);
                        //alert("成功")
                        //window.opener.sendToActionScript();
                        //window.close();

                    }
                },
                complete: function (xmlHttpRequest, textStatus) {

                },
                error: function (e) {
                    alert(e);
                    alert("error");
                }
            });
        }
        catch (e) {
            alert("exception: " + e.Message);
        }
    }
}

function DownloadFile(isWhat) {
    window.open("downloadFile.aspx?lang=" + isWhat);
}